//
//  AddExtensionViewController.m
//  SIPphone
//
//  Created by wrt on 15-2-6.
//  Copyright (c) 2015年 wrtsoft. All rights reserved.
//

#import "AddExtensionViewController.h"
#import "TPKeyboardAvoidingScrollView.h"
#import "SVProgressHUD.h"

@interface AddExtensionViewController ()

@end

@implementation AddExtensionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    float systemVersion = iOSValue;
    if (systemVersion >= 7.0)
    {
        self.edgesForExtendedLayout=UIRectEdgeNone;
    }
    [self.scrollView contentSizeToFit];
    
    self.title = NSLocalizedString(@"adding Binding", @"添加设备绑定 Adding Binding");
    [[NetPostClass shareNetPostClass] setDelegate:self];
}

- (IBAction)sureButtonPressed:(id)sender
{
    
    if (self.devPassword.text.length > 0 && self.devNumTextField.text.length > 0)
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *tempUserName = [defaults valueForKey:@"SAVEUSERNAME"];
        NSString *tempPassWord = [defaults valueForKey:@"SAVEPASSWORD"];
        
        [[NetPostClass shareNetPostClass] postBindExtensionWithUserName:tempUserName andPassWord:tempPassWord andDeviceCode:self.devNumTextField.text andDevicePassword:self.devPassword.text andDeviceType:0 andDeviceName:self.nameTextFiled.text andDev:@"WRTSIP-W_TALK-W_MSG-@IOSPHONE-RVTI@-V1.00.00"];
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:NSLocalizedString(@"please fill out the complete contents", @"请将内容填写完整 Please fill out the complete contents")];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - NetPostDelegateMethod
/**
 *  post数据成功回调
 *
 *  @param comeBackDic 返回来的字典数据
 *  @param tagNumber   post的tag标识
 */
- (void)postSuccessfulWithString:(NSDictionary *)comeBackDic withTag:(int)tagNumber
{
    if (tagNumber == 10)
    {
        [SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"added successfully", @"添加成功 Added successfully")];
        //延时0.5秒后返回
        double delayInSeconds1 = 0.5;
        dispatch_time_t popTime1 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds1 * NSEC_PER_SEC));
        dispatch_after(popTime1, dispatch_get_main_queue(), ^(void)
                       {
                           [self.navigationController popViewControllerAnimated:YES];
                       });
    }
}

/**
 *  post失败返回数据
 *
 *  @param faileString 失败的原因
 *  @param tagNumber   post的tag标识
 */
- (void)postFailedWithString:(NSString *)faileString withTag:(int)tagNumber
{
    //post失败返回的失败原因
    dispatch_async(dispatch_get_main_queue(),^{
        if ([faileString isEqualToString:@"the deivce is talk device but is not a room machine"])
        {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@\n%@",NSLocalizedString(@"add Failed", @"添加失败 Add Failed"),NSLocalizedString(@"the device is not room extension", @"该设备不是房间分机 The device is not room extension")]];
        }
        else if ([faileString isEqualToString:@"the deivce is bind too many users"])
        {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@\n%@",NSLocalizedString(@"add Failed", @"添加失败 Add Failed"),NSLocalizedString(@"the device binding excessive number", @"该设备绑定次数过多 The device binding excessive number")]];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:NSLocalizedString(@"add Failed", @"添加失败 Add Failed")];
        }
    });
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
